using System;
using System.Linq;
using JNPF.Utils;
using System.Threading.Tasks;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using JNPF.Basics.Models;

namespace JNPF.Basics.Logic
{
    public class jiushiZzhuserService : DataFactory, jiushiZzhuserIService
    {
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<jiushiZzhuserEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<jiushiZzhuserEntity>(m => m.Id == id);
            }
        }

        /// <summary>
        /// 根据公众号ID获取用户
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public async Task<jiushiZzhuserEntity> GetInfoByOpenid(string openId)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<jiushiZzhuserEntity>(m => m.OpenId == openId);
            }
        }

        /// <summary>
        /// 根据小程序ID获取用户
        /// </summary>
        /// <param name="xcxOpenId"></param>
        /// <returns></returns>
        public async Task<jiushiZzhuserEntity> GetInfoByXcxOpenId(string xcxOpenId)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<jiushiZzhuserEntity>(m => m.XcxOpenId == xcxOpenId);
            }
        }

        /// <summary>
        /// 根据UUID获取用户ID
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public async Task<jiushiZzhuserEntity> GetInfoByuuid(string uuid)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<jiushiZzhuserEntity>(m => m.Uuid == uuid);
            }
        }

        /// <summary>
        /// 根据UUID获取用户ID
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public async Task<jiushiZzhuserEntity> GetInfoByUserId(string userId)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<jiushiZzhuserEntity>(m => m.UserId == userId);
            }
        }




        /// <summary>
        /// 列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<jiushiZzhuserEntity>> GetPageList(jiushiZzhuserListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<jiushiZzhuserEntity>();
                var queryParam = input.queryJson.ToObject();
                return await db.GetIQueryable<jiushiZzhuserEntity>().Where(queryWhere).OrderByDescending(t => t.CreatorTime).GetPageResultAsync(input);
            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(jiushiZzhuserEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(jiushiZzhuserEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(jiushiZzhuserEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<List<jiushiZzhuserEntity>> GetList(jiushiZzhuserListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<jiushiZzhuserEntity>();
                return await db.GetIQueryable<jiushiZzhuserEntity>().Where(queryWhere).OrderByDescending(t => t.CreatorTime).ToListAsync();
            }
        }


        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<List<jiushiZzhuserEntity>> GetListByType(int type)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<jiushiZzhuserEntity>();
                queryWhere = queryWhere.And(t => t.Type == 1);
                return await db.GetIQueryable<jiushiZzhuserEntity>().Where(queryWhere).OrderByDescending(t => t.CreatorTime).ToListAsync();
            }
        }


    }
}